package com.neu.carbon.mes.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.neu.carbon.mes.mapper.CheckStandardMapper;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.mapper.WmsMaterialInfoMapper;
import com.neu.common.enums.SerialNoPrefix;
import com.neu.common.utils.DateUtils;
import com.neu.common.utils.SecurityUtils;
import com.neu.smty.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neu.carbon.mes.mapper.MesMaterialCheckMapper;
import com.neu.carbon.mes.domain.MesMaterialCheck;
import com.neu.carbon.mes.service.IMesMaterialCheckService;

/**
 * 物料检验单Service业务层处理
 * 
 * @author neuedu
 * @date 2024-11-25
 */
@Service
public class MesMaterialCheckServiceImpl implements IMesMaterialCheckService 
{
    @Autowired
    private MesMaterialCheckMapper mesMaterialCheckMapper;

    @Autowired
    private CheckStandardMapper checkStandardMapper;

    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;

    /**
     * 查询物料检验单
     * 
     * @param id 物料检验单ID
     * @return 物料检验单
     */
    @Override
    public MesMaterialCheck selectMesMaterialCheckById(Long id)
    {
        MesMaterialCheck materialCheck = mesMaterialCheckMapper.selectMesMaterialCheckById(id);
        Long materialId = materialCheck.getMaterialId();
        if(materialId!=null){
            WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(materialId);
            if(materialInfo!=null){
                String unit = materialInfo.getUnit();
                String model = materialInfo.getModel();
                String specification = materialInfo.getSpecification();

                if(unit!=null){
                    materialCheck.setUnit(unit);
                }
                if(model!=null){
                    materialCheck.setModel(model);
                }
                if(specification!=null){
                    materialCheck.setSpecification(specification);
                }
            }

        }

        Long checkStandardId = materialCheck.getCheckStandardId();
        if(checkStandardId!=null){
            String name = checkStandardMapper.selectItemById(checkStandardId);
            materialCheck.setCheckItemName(name);
        }
        return materialCheck;
    }

    /**
     * 查询物料检验单列表
     * 
     * @param mesMaterialCheck 物料检验单
     * @return 物料检验单
     */
    @Override
    public List<MesMaterialCheck> selectMesMaterialCheckList(MesMaterialCheck mesMaterialCheck)
    {
        List<MesMaterialCheck> list = mesMaterialCheckMapper.selectMesMaterialCheckList(mesMaterialCheck);
        if(list!=null){
            for (MesMaterialCheck materialCheck : list) {
                Long checkStandardId = materialCheck.getCheckStandardId();
                if(checkStandardId!=null){
                    String name = checkStandardMapper.selectItemById(checkStandardId);
                    materialCheck.setCheckItemName(name);
                }

                Long materialId = materialCheck.getMaterialId();
                if(materialId!=null){
                    WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(materialId);
                    if(materialInfo!=null){
                        String unit = materialInfo.getUnit();
                        String model = materialInfo.getModel();
                        String specification = materialInfo.getSpecification();
                        String name = materialInfo.getName();

                        if(unit!=null){
                            materialCheck.setUnit(unit);
                        }
                        if(model!=null){
                            materialCheck.setModel(model);
                        }
                        if(specification!=null){
                            materialCheck.setSpecification(specification);
                        }
                        if(name!=null){
                            materialCheck.setMaterialName(name);
                        }
                    }
                }
            }
        }

        return list;
    }

    /**
     * 新增物料检验单
     * 
     * @param mesMaterialCheck 物料检验单
     * @return 结果
     */
    @Override
    public int insertMesMaterialCheck(MesMaterialCheck mesMaterialCheck)
    {
        mesMaterialCheck.setSerialNo(CodeUtil.getSerialNo(SerialNoPrefix.WLJY));
        mesMaterialCheck.setCheckUser(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        mesMaterialCheck.setCheckDate(DateUtils.getNowDate());
        return mesMaterialCheckMapper.insertMesMaterialCheck(mesMaterialCheck);
    }

    /**
     * 修改物料检验单
     * 
     * @param mesMaterialCheck 物料检验单
     * @return 结果
     */
    @Override
    public int updateMesMaterialCheck(MesMaterialCheck mesMaterialCheck)
    {
        return mesMaterialCheckMapper.updateMesMaterialCheck(mesMaterialCheck);
    }

    /**
     * 批量删除物料检验单
     * 
     * @param ids 需要删除的物料检验单ID
     * @return 结果
     */
    @Override
    public int deleteMesMaterialCheckByIds(Long[] ids)
    {
        return mesMaterialCheckMapper.deleteMesMaterialCheckByIds(ids);
    }

    /**
     * 删除物料检验单信息
     * 
     * @param id 物料检验单ID
     * @return 结果
     */
    @Override
    public int deleteMesMaterialCheckById(Long id)
    {
        return mesMaterialCheckMapper.deleteMesMaterialCheckById(id);
    }
}
